package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import web.mb.InfoOffreMB;
import web.mb.loginnMB;

public class loginFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {

		HttpServletRequest req=(HttpServletRequest) arg0;
		HttpServletResponse resp=(HttpServletResponse) arg1;
		loginnMB session=(loginnMB) req.getSession().getAttribute("loginnMB");
		InfoOffreMB sessioninfo=(InfoOffreMB) req.getSession().getAttribute("infoOffreMB");

		String url=req.getRequestURI();
		/*
	      A.if request is for forum or logout and there's no session,redirect the request to login.xhtml
	      B.if request is for register or login and there's a session,redirect the request to forum.xhtml
	      C.if request is logout and there's a session,remove the session to login.xhtml
		 */
		
		////si l'utilisateur veut accéder àla page transport 
		if(url.indexOf("myTransport")>=0)
		{
			if(session==null || !session.isLong)
			{
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/login.xhtml");	
			}
			else 
			{
				if(session.admin)
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/login.xhtml");
				else
				{
					if(session.C.getTranExp().compareTo("Tra")==0)
						resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/UsersSpace/CarriersSpace/Test.xhtml");
					else
						resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/UsersSpace/ShippersSpace/Test.xhtml");
						
						
				}
			}
			
				
		}
		
		
		//////////////////////si l'utilisateur veut accéder à la page InfoOffre.xhtml
		
		if(url.indexOf("InfoOffre.xhtml")>=0)
		{System.out.println("heeeeeeeeeeeere"+sessioninfo==null);
			
			if(sessioninfo==null || sessioninfo.OFFRE==null)
			{
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/chercherOffre.xhtml");
			}
		}
		
		/////////////////////si l'utilisateur veut accéder à la page Paiement.xhtml
		/*if(url.indexOf("Paiement.xhtml")>=0)
		{
			
			if(sessioninfo==null || sessioninfo.OFFRE==null || session==null || !session.isLong )
			{
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/ChercherOffre.xhtml");
			}
		}*/
		
		///////////////////si l'utilisateur veut accéder à la page biderr.xhtml
		if(url.indexOf("biderr.xhtml")>=0)
		{
			
			if(sessioninfo==null || sessioninfo.OFFRE==null || sessioninfo.DECLARATION==null || session==null || !session.isLong)
			{
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/ChercherOffre.xhtml");
			}
		}
		
		if(session==null || !session.isLong)
		{
			
			/*if(url.indexOf("forum.xhtml")>=0 || url.indexOf("logout.xhtml")>=0)
			{
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/TestLogin/login.xhtml");
			}
			else
			{
				arg2.doFilter(req, resp);
			}*/
			
			if(url.indexOf("welcome.xhtml")>=0 || url.indexOf("logout.xhtml")>=0  || url.indexOf("Home.xhtml")>=0 )
			{
				if(url.indexOf("Home.xhtml")>=0)
					resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/LoginAdmin.xhtml");
				else
					resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/login.xhtml");	
			}
			else
			{
				arg2.doFilter(req, resp);
			}
			
			
		}
		else
		{
			System.out.println("sess"+session.C.getTranExp()+"--"+session.C.getEmail());
			
			if(url.indexOf("login.xhtml")>=0 || url.indexOf("loginAdmin.xhtml")>=0)
			{
				//resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/TestLogin/forum.xhtml");
				if(session.admin)
				{
					resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/Admin/Home.xhtml");
				}
				else
				{
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/welcome.xhtml");
				}
				
			}
			else if(url.indexOf("logout.xhtml")>=0)
			{
				
				//resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/TestLogin/login.xhtml");
				if(session.admin)
				{
					
					req.getSession().removeAttribute("loginnMB");
					req.getSession().removeAttribute("infoOffreMB");
					
				resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/login.xhtml");
				}
				else
				{
					req.getSession().removeAttribute("loginnMB");
					req.getSession().removeAttribute("infoOffreMB");
					resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/LoginAdmin.xhtml");
				}
				
			}
			/*else if(url.indexOf("Home.xhtml")>=0)
			{
				
				if(!session.admin)
				{
					req.getSession().removeAttribute("loginnMB");
					resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/LoginAdmin.xhtml");
				}
				
				
				
			}*/
			/*else if(url.indexOf("welcome.xhtml")>=0 || url.indexOf("AvantPaiement.xhtml")>=0 )
			{
				if(session.admin)
				{
					req.getSession().removeAttribute("loginnMB");
					resp.sendRedirect(req.getServletContext().getContextPath()+"/faces/frontOffice/Login/LoginAdmin.xhtml");
				}
			}*/
			else
			{
				arg2.doFilter(req, resp);
			}
				
		}
			
		
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
